﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VideoClassLibrary.Extension;
using VideoClassLibrary.Model;
using VideoClassLibrary.BusinessLogicLayer;
public partial class ClassRoomManagement_UpdateRoom : AdminUserBase
{
    private int rid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            ClassRoomModule rm = new ClassRoomModule();
            IPCInfoModule ipcInfoModule=new IPCInfoModule();
            SVR_IPC_Relation svrIpcRelation=new SVR_IPC_Relation();
            rid = Convert.ToInt32(Request.QueryString["RID"].ToString());
            IPCInfo_Table ipcInfoTable = ipcInfoModule.GetIpcInfoModelById(rid);
            if(ipcInfoTable!=null)
            {
                svrIpcRelation = rm.GetByIPCID(ipcInfoTable.IPCID);
                if (svrIpcRelation != null)
                {

                    txtSvrPort.Value = svrIpcRelation.RelationPipe.ToString();
                    hfPort.Value = svrIpcRelation.RelationPipe.ToString();
                    hfSvr.Value = svrIpcRelation.SVRID.ToString();
                }
            }
            
            V_room ro = rm.GetRoomModelById(rid);
            //IPCInfo_Table infoTable=
            txtVR_roomName.Text = ro.VR_roomName;
            dlSchool.SelectedValue = ro.SchoolID.ToString();
            DropDownList1.SelectedValue = ro.VR_state.ToString();
            txtVR_roomCode.Text = ro.VR_roomCode;
            txtVR_cameraPN.Text = ro.VR_cameraPN;
            txtVR_cameraCode.Text = ro.VR_cameraCode;
            txtVR_cameraImgSize.Text = ro.VR_cameraImgSize;
            txtIPCIp.Text = ipcInfoTable.IPCIp;
            txtIPCPort.Text = ipcInfoTable.IPCPort.ToString();
            txtIPCPwd.Text = ipcInfoTable.IPCPwd;
            txtIPCUser.Text = ipcInfoTable.IPCUser;
            hfIPCID.Value = ipcInfoTable.IPCID.ToString();
            TeacherModule ta = new TeacherModule();
            List<V_School> vs = new List<V_School>();
          
            vs = ta.GetSchool();
            dlSchool.DataSource = vs;
            dlSchool.DataBind();
            if (this.CurrentUser.SchoolID == 0)
            {
            }
            else
            {
                dlSchool.SelectedValue = this.SchoolID.ToString();
                dlSchool.Enabled = false;
                //摄像头控制，运营商能修改
                DropDownList1.SelectedValue = "1";
                DropDownList1.Enabled = false;
            }
            #region 绑定SVR列表
            SVR_IPC_Module svr = new SVR_IPC_Module();
            DataSet dsSvrList = svr.GetSVRInfoList("");
            ddlSVRList.DataSource = dsSvrList;
            ddlSVRList.DataValueField = "SVRID";
            ddlSVRList.DataTextField = "SVRName";
            ddlSVRList.DataBind();

            #endregion
        }
    }
  
    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        ClassRoomModule ra = new ClassRoomModule();
        IPCInfoModule ipcInfoModule = new IPCInfoModule();
        IPCInfo_Table infoTable=new IPCInfo_Table();
        SVR_IPC_Relation svrIpcRelation=new SVR_IPC_Relation();

        V_room room = new V_room();
        room.ID =Convert.ToInt32(Request.QueryString["RID"].ToString());
        room.Create_time = DateTime.Now;
        room.update_time = DateTime.Now;
        room.SchoolID = int.Parse(dlSchool.SelectedValue);
        room.VR_state = int.Parse(DropDownList1.SelectedValue);
        room.VR_roomName = this.txtVR_roomName.Text;
        room.VR_roomCode = this.txtVR_roomCode.Text;
        room.VR_cameraPN = this.txtVR_cameraPN.Text;
        room.VR_cameraCode = this.txtVR_cameraCode.Text;
        room.VR_cameraImgSize = this.txtVR_cameraImgSize.Text;

        infoTable.IPCID =Convert.ToInt32(hfIPCID.Value);
        infoTable.IPCIp = txtIPCIp.Text;
        infoTable.IPCPort =!string.IsNullOrEmpty(txtIPCPort.Text) ? StringUtils.IsNumeric(txtIPCPort.Text) ? Convert.ToInt32(txtIPCPort.Text) : 0 : 0;
        infoTable.IPCName = txtVR_roomName.Text;
        infoTable.IPCUser = txtIPCUser.Text;
        infoTable.IPCPwd = txtIPCPwd.Text;
        svrIpcRelation.RelationPipe = !string.IsNullOrEmpty(txtSvrPort.Value) ? StringUtils.IsNumeric(txtSvrPort.Value) ? Convert.ToInt32(txtSvrPort.Value) : 0 : 0;
        svrIpcRelation.RelationComment = txtVR_roomCode.Text;
        svrIpcRelation.IPCID = Convert.ToInt32(hfIPCID.Value);
        svrIpcRelation.SVRID = Convert.ToInt32(ddlSVRList.SelectedValue);
        if(hfPort.Value!=svrIpcRelation.RelationPipe.ToString()||hfSvr.Value!= svrIpcRelation.SVRID.ToString())
        {
            DataSet dataSet = ra.ChkSvrPort(svrIpcRelation.RelationPipe.ToString(), svrIpcRelation.SVRID.ToString(), svrIpcRelation.SVRID.ToString());
            if (dataSet.Tables[0].Rows.Count > 0)
            {
                Page.RegisterStartupScript("",
                                           "<script>alert('对不起，" + ddlSVRList.SelectedItem.Text + "的端口号：" +
                                           svrIpcRelation.RelationPipe.ToString() + "，被" +
                                           dataSet.Tables[0].Rows[0]["Vs_name"] + "学校的：" +
                                           dataSet.Tables[0].Rows[0]["VR_roomName"] + "教室占用！');</script>");
                return;
            }

        }
     
        if ( ra.UpdateRoom(room)== 1&&ipcInfoModule.UpdateInfoTable(infoTable)==1&& ra.UpdateIPCRelationTable(svrIpcRelation)==1)
        {
            
            PageHelper.AlterMsgAndRedirect("修改成功", "SearchRoomList.aspx");
            
        }
        else
        {
         
             lblMess.Text = "修改失败";
        }
    }

    protected void renew_Click(object sender, EventArgs e)
    {
        txtVR_roomName.Text = null;
        txtVR_roomCode.Text= null;
        txtVR_cameraPN.Text= null;
        txtVR_cameraCode.Text= null;
        txtVR_cameraImgSize.Text = null;
    }
}
